package com.sheng.whyclass.person.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.sheng.whyclass.R;
import com.sheng.whyclass.work.fragment.AnswerFragment;
import com.sheng.whyclass.person.fragment.StudentInfoFragment;
import com.sheng.whyclass.work.fragment.WorkFinishedFragment;
import com.sheng.whyclass.work.fragment.WorkUnfinishFragment;
import com.sheng.whyclass.person.view.MainActNavigateView;

public class StudentMainActivity extends FragmentActivity {

    private StudentInfoFragment studentInfoFragment = null;
    private WorkFinishedFragment workFinishedFragment = null;
    private WorkUnfinishFragment workUnfinishFragment = null;
    private AnswerFragment answerFragment = null;

    private NavigateTab curBottomTab = null;

    private MainActNavigateView mainActNavigateView;

    public enum NavigateTab {
        NAVIGATE_STUDENT_INFO,
        NAVIGATE_WORK_UNFINISH,
        NAVIGATE_WORK_FINISHED,
        NAVIGATE_ANSWER
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student_main);

        studentInfoFragment = new StudentInfoFragment();
        workFinishedFragment = new WorkFinishedFragment();
        workUnfinishFragment = new WorkUnfinishFragment();
        answerFragment = new AnswerFragment();

        curBottomTab = NavigateTab.NAVIGATE_STUDENT_INFO;
        initView();
        initFragment();
    }

    private void initView() {
        mainActNavigateView = (MainActNavigateView) findViewById(R.id.mainActNavigateView);
        mainActNavigateView.setCallback(new MainActNavigateView.ICallback() {

            @Override
            public void onClicked(byte component) {
                switch (component) {
                    case MainActNavigateView.Component.NAVIGATE_STUDENT_INFO: {
                        switchToNavTab(NavigateTab.NAVIGATE_STUDENT_INFO);
                        break;
                    }
                    case MainActNavigateView.Component.NAVIGATE_WORK_UNFINISH: {
                        switchToNavTab(NavigateTab.NAVIGATE_WORK_UNFINISH);
                        break;
                    }
                    case MainActNavigateView.Component.NAVIGATE_WORK_FINISHED: {
                        switchToNavTab(NavigateTab.NAVIGATE_WORK_FINISHED);
                        break;
                    }
                    case MainActNavigateView.Component.NAVIGATE_ANSWER: {
                        switchToNavTab(NavigateTab.NAVIGATE_ANSWER);
                        break;
                    }
                }
            }
        });
    }

    private void initFragment() {
        switchToNavTab(curBottomTab);
    }

    public void switchToNavTab(NavigateTab tab) {
        if (tab == null) {
            return;
        }
        curBottomTab = tab;
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager != null) {
            if (tab == NavigateTab.NAVIGATE_STUDENT_INFO) {

                String wifiTag = StudentInfoFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            studentInfoFragment, wifiTag);
                }

                ft.show(studentInfoFragment);
                ft.hide(workUnfinishFragment);
                ft.hide(workFinishedFragment);
                ft.hide(answerFragment);
                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateView.Component.NAVIGATE_STUDENT_INFO);
            } else if (tab == NavigateTab.NAVIGATE_WORK_UNFINISH) {

                String wifiTag = WorkUnfinishFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            workUnfinishFragment, wifiTag);
                }

                ft.hide(studentInfoFragment);
                ft.show(workUnfinishFragment);
                ft.hide(workFinishedFragment);
                ft.hide(answerFragment);
                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateView.Component.NAVIGATE_WORK_UNFINISH);
            } else if (tab == NavigateTab.NAVIGATE_WORK_FINISHED) {

                String wifiTag = WorkFinishedFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            workFinishedFragment, wifiTag);
                }

                ft.hide(studentInfoFragment);
                ft.hide(workUnfinishFragment);
                ft.show(workFinishedFragment);
                ft.hide(answerFragment);
                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateView.Component.NAVIGATE_WORK_FINISHED);
            } else if (tab == NavigateTab.NAVIGATE_ANSWER) {

                String wifiTag = AnswerFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            answerFragment, wifiTag);
                }

                ft.hide(studentInfoFragment);
                ft.hide(workUnfinishFragment);
                ft.hide(workFinishedFragment);
                ft.show(answerFragment);
                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateView.Component.NAVIGATE_ANSWER);
            }
        }
    }


}
